import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../views/LoginView.vue'
import AboutView from '../views/AboutView.vue'
import LayoutView from '@/layout'


Vue.use(VueRouter)

const routes = [
  {
    path: '/login',  // 登录页路径
    component: LoginView
  },
  {
    path: '/about',  // 登录页路径
    component: AboutView
  },
  {
    path: '/',       // 首页路径（需登录后访问）
	component: LayoutView,
	redirect: '/home',
    children: [
          {
            path: 'home',
            name: 'HomeView',
            component: () => import('@/views/HomeView'),
            meta: { title: '首页', icon: 'dashboard' }
          }
        ]
  },
  {
  	path: '/movies',
  	component: LayoutView,
  	redirect: '/movies/list',
  	meta: {
  		title: '电影管理',
  		icon: 'film'
  	},
  	children: [{
				path: 'list',
				name: 'MovieList',
				component: () => import('@/views/movies/ListView'),
				meta: {
					title: '电影列表'
				}
			},
			{
				path: 'schedule',
				name: 'Schedule',
				component: () => import('@/views/movies/Schedule'),
				meta: {
					title: '排片管理'
				}
			}

		]
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {

  // 设置标题（优先使用目标路由的 meta.title，否则使用默认值）
  const title = to.meta.title || '电影管理系统';
  document.title = `${title} - 电影管理系统`;

  // 放行登录页
  if (to.path === '/login') return next();

  // 检查登录状态
  const userInfo = localStorage.getItem('userInfo');

  if (userInfo != 'undefined') {
    next(); // 已登录，放行
  } else {
    next('/login'); // 未登录，跳转登录页
  }
});

export default router
